﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using nwrole.Orchestrator.site.Utils;
using nwrole.lib;
using nwrole.Orchestrator.site.Models;

namespace nwrole.Orchestrator.site.Controllers
{
    public class SupervisorController : Controller
    {
        //
        // GET: /Supervisor/

        public ActionResult Index()
        {
            SupervisorInfo[] supervisorDatas;
            //using(TcpRelayChannel<IOrchestratorCtrlChannel> tcpRelayChannel = new TcpRelayChannel<IOrchestratorCtrlChannel>("nwrole/OrchestratorCtrl")) {
            using(TcpRelayChannel<IOrchestratorCtrlChannel> tcpRelayChannel = new TcpRelayChannel<IOrchestratorCtrlChannel>(SiteSettings.OrchestratorSBServiceNamespace, SiteSettings.OrchestratorSBServicePath, SiteSettings.OrchestratorSBIssuerName, SiteSettings.OrchestratorSBIssuerSecret)) {
                supervisorDatas = tcpRelayChannel.Channel.GetRegisteredSupervisors();
            }

            if (supervisorDatas == null)
                return View(new List<SupervisorInfo>());

            return View(supervisorDatas.AsEnumerable());
        }

        public ActionResult Details(Guid id) {
            SupervisorDetailsViewModel viewModel = new SupervisorDetailsViewModel();
            //using (TcpRelayChannel<IOrchestratorCtrlChannel> tcpRelayChannel = new TcpRelayChannel<IOrchestratorCtrlChannel>("nwrole/OrchestratorCtrl")) {
            using (TcpRelayChannel<IOrchestratorCtrlChannel> tcpRelayChannel = new TcpRelayChannel<IOrchestratorCtrlChannel>(SiteSettings.OrchestratorSBServiceNamespace, SiteSettings.OrchestratorSBServicePath, SiteSettings.OrchestratorSBIssuerName, SiteSettings.OrchestratorSBIssuerSecret))
            {
                SupervisorInfo[] supervisorInfos = tcpRelayChannel.Channel.GetRegisteredSupervisors();
                viewModel.SupervisorInfo = supervisorInfos.SingleOrDefault(s => s.Id == id);
                if (viewModel.SupervisorInfo != null)
                    viewModel.ServicesInfo = tcpRelayChannel.Channel.GetSupervisorServicesInfo(viewModel.SupervisorInfo.Id);
                
                if(viewModel.ServicesInfo == null)
                    viewModel.ServicesInfo = new List<ServiceInfo>();

                /*SupervisorData[] supervisorDatas = tcpRelayChannel.Channel.GetSupervisors();
                viewModel.SupervisorData = supervisorDatas.SingleOrDefault(s => s.Id == id);
                if(viewModel.SupervisorData != null)
                    viewModel.ServicesInfo = tcpRelayChannel.Channel.GetSupervisorServicesInfo(viewModel.SupervisorData.Id);
                else
                    viewModel.ServicesInfo = new List<ServiceInfo>();*/
            }

            return View(viewModel);
        }

        public ActionResult RestartSupervisor(Guid id)
        {
            using (TcpRelayChannel<IOrchestratorCtrlChannel> tcpRelayChannel = new TcpRelayChannel<IOrchestratorCtrlChannel>(SiteSettings.OrchestratorSBServiceNamespace, SiteSettings.OrchestratorSBServicePath, SiteSettings.OrchestratorSBIssuerName, SiteSettings.OrchestratorSBIssuerSecret))
            {
                tcpRelayChannel.Channel.RestartSupervisor(id);
            }

            return RedirectToAction("Index");
        }

        public ActionResult RestartService(Guid id)
        {
            using (TcpRelayChannel<IOrchestratorCtrlChannel> tcpRelayChannel = new TcpRelayChannel<IOrchestratorCtrlChannel>(SiteSettings.OrchestratorSBServiceNamespace, SiteSettings.OrchestratorSBServicePath, SiteSettings.OrchestratorSBIssuerName, SiteSettings.OrchestratorSBIssuerSecret))
            {
                tcpRelayChannel.Channel.RestartService(id);
            }

            return RedirectToAction("Details");
        }

        public ActionResult StopService(Guid id)
        {
            Guid supervisorId;

            using (TcpRelayChannel<IOrchestratorCtrlChannel> tcpRelayChannel = new TcpRelayChannel<IOrchestratorCtrlChannel>(SiteSettings.OrchestratorSBServiceNamespace, SiteSettings.OrchestratorSBServicePath, SiteSettings.OrchestratorSBIssuerName, SiteSettings.OrchestratorSBIssuerSecret))
            {
                tcpRelayChannel.Channel.StopService(id);
                ServiceInfo info = tcpRelayChannel.Channel.GetServiceInfo(id);
                supervisorId = info.SupervisorId;
            }

            return RedirectToAction("Details", new { id = supervisorId});
        }

        public ActionResult StartService(Guid id)
        {
            Guid supervisorId;

            using (TcpRelayChannel<IOrchestratorCtrlChannel> tcpRelayChannel = new TcpRelayChannel<IOrchestratorCtrlChannel>(SiteSettings.OrchestratorSBServiceNamespace, SiteSettings.OrchestratorSBServicePath, SiteSettings.OrchestratorSBIssuerName, SiteSettings.OrchestratorSBIssuerSecret))
            {
                tcpRelayChannel.Channel.StartService(id);
                ServiceInfo info = tcpRelayChannel.Channel.GetServiceInfo(id);
                supervisorId = info.SupervisorId;
            }

            return RedirectToAction("Details", new { id = supervisorId });
        }
    }
}
